package com.hr.牛客网剑指offer;

/**
 * @author zhz
 * @date 2020/08/18
 **/
public class JZ1_二维数组中的查找 {
    //暴力法(遍历所有)
    public boolean find1(int target, int [][] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                if (target==array[i][j]){
                    return true;
                }
            }
        }
        return false;
    }
    /**
     * 把每一行看成有序递增的数组，
     * 利用二分查找，
     * 通过遍历每一行得到答案，
     * 时间复杂度是nlogn
     */
    public boolean find2(int target, int [][] array) {
        for (int i = 0; i < array.length; i++) {
            int low=0;
            int high=array[i].length-1;
            while (low<high){
                int mid = low + ((high - low) >> 1);
                if (target>array[i][mid]){
                    low=mid+1;
                }else if (target<array[i][mid]){
                    high=mid-1;
                }else {
                    return true;
                }
            }
        }
        return false;
    }


}
